﻿using CongregacaoOs.Business;
using CongregacaoOs.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CongregacaoOs.WebForm.Cadastros
{
    public partial class Agenda : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DadosGerais"] == null) Response.Redirect("~/Cadastros/Account/Login.aspx");

            if (!IsPostBack)
            {

                AtualizarGridView();
                AtualizarDropRegionalFiltro();
            }
        }

        public void AtualizarGridView()
        {
            ccPessoaBus bus = new ccPessoaBus();
            ccPessoa ent = new ccPessoa();

            ent.nome = txtBuscarNome.Text.Trim();
            ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
            ent.idDistrital = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;
            ent.idAdm = dropAdmFiltro.SelectedValue != "" ? Convert.ToInt32(dropAdmFiltro.SelectedValue) : 0;
            ent.idSetor = dropSetorFiltro.SelectedValue != "" ? Convert.ToInt32(dropSetorFiltro.SelectedValue) : 0;

            if (radioStatusUsu.SelectedValue != "0")
                ent.status = radioStatusUsu.SelectedValue == "1" ? true : false;

            gvServo.DataSource = bus.PessoaSelect(ent);
            gvServo.DataBind();
        }

        public void AtualizarDropRegionalFiltro()
        {
            using (ccRegionalBus bus = new ccRegionalBus())
            {
                ccRegional ent = new ccRegional();
                ent.idEstado = 0;
                ent.nome = "";
                ent.idPais = 0;
                dropRegionalFiltro.DataSource = bus.RegionalSelect(ent);
                dropRegionalFiltro.DataBind();
            }

        }

        public void AtualizarDropDistritalFiltro()
        {
            using (ccDistritalBus bus = new ccDistritalBus())
            {
                ccDistrital ent = new ccDistrital();

                if (dropRegionalFiltro.SelectedValue != "")
                {
                    ent.idRegional = Convert.ToInt32(dropRegionalFiltro.SelectedValue);
                    dropDistritalFiltro.DataSource = bus.DistritalSelect(ent);
                    dropDistritalFiltro.DataBind();
                }
                else
                {
                    dropDistritalFiltro.Items.Clear();
                    dropAdmFiltro.Items.Clear();
                    dropSetorFiltro.Items.Clear();
                }

            }
        }

        public void AtualizarDropAdmFiltro()
        {
            if (dropRegionalFiltro.SelectedValue != "" && dropDistritalFiltro.SelectedValue != "")
            {
                using (ccAdmBus bus = new ccAdmBus())
                {

                    ccAdm ent = new ccAdm();

                    ent.idRegional = dropRegionalFiltro.SelectedValue != "" ? Convert.ToInt32(dropRegionalFiltro.SelectedValue) : 0;
                    ent.idDistrital = dropDistritalFiltro.SelectedValue != "" ? Convert.ToInt32(dropDistritalFiltro.SelectedValue) : 0;

                    dropAdmFiltro.DataSource = bus.AdmSelect(ent);
                    dropAdmFiltro.DataBind();

                }

            }
            else
            {
                dropAdmFiltro.Items.Clear();
                dropSetorFiltro.Items.Clear();
            }
        }

        public void AtualizarDropSetorFiltro()
        {
            if (dropAdmFiltro.SelectedValue == "")
            {
                dropSetorFiltro.Items.Clear();
                return;
            }

            using (ccSetorBus bus = new ccSetorBus())
            {
                ccSetor ent = new ccSetor();

                if (dropAdmFiltro.SelectedValue != "")
                {
                    dropSetorFiltro.DataSource = bus.SetorSelectByAdm(Convert.ToInt32(dropAdmFiltro.SelectedValue));
                    dropSetorFiltro.DataBind();

                }
                else
                {
                    dropSetorFiltro.Items.Clear();
                }
            }
        }

        protected void dropRegionalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropDistritalFiltro();
            AtualizarGridView();
        }

        protected void dropDistritalFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropAdmFiltro();
            AtualizarGridView();
        }

        protected void dropAdmFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarDropSetorFiltro();
            AtualizarGridView();
        }

        protected void dropSetorFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void radioStatusUsu_SelectedIndexChanged(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

        protected void dropRegionalFiltro_DataBound(object sender, EventArgs e)
        {
            dropRegionalFiltro.Items.Insert(0, new ListItem("Escolha a Regional...", String.Empty));
        }

        protected void dropDistritalFiltro_DataBound(object sender, EventArgs e)
        {
            dropDistritalFiltro.Items.Insert(0, new ListItem("Escolha a Distrital...", String.Empty));
        }

        protected void dropAdmFiltro_DataBound(object sender, EventArgs e)
        {
            dropAdmFiltro.Items.Insert(0, new ListItem("Escolha a Administração...", String.Empty));
        }

        protected void dropSetorFiltro_DataBound(object sender, EventArgs e)
        {
            dropSetorFiltro.Items.Insert(0, new ListItem("Escolha o Setor...", String.Empty));
        }


        protected void gvServo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AtualizarGridView();
            gvServo.PageIndex = e.NewPageIndex;
            gvServo.DataBind();
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            AtualizarGridView();
        }

    }
}